package com.aaa.controller;

import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("test")
@CrossOrigin
public class TestController {
    @Resource
    RedisTemplate redisTemplate;

    @RequestMapping("test")
    public String m1(){
        ValueOperations operations = redisTemplate.opsForValue();

        Random random = new Random();
        int num = 1000 + random.nextInt(9000);//随机4位数字

        operations.set("code",num,60, TimeUnit.SECONDS);
        String code = operations.get("code").toString();
        System.out.println("code = " + code);
        return code;
    }

    @RequestMapping("GetCode")
    public int m2(String Code){
        ValueOperations operations = redisTemplate.opsForValue();
        Object String = operations.get("code");

        if (String!=null) {
            java.lang.String s = String.toString();
            if(s.equals(Code)){
                return 2;
            }
        }
        if(String==null){
            return 1;//过期
        }
        System.out.println("Code = " + Code);
        return 0;//错
    }


    /*@RequestMapping("test2")
    public void m2(String value){
        BoundValueOperations code = redisTemplate.boundValueOps("code");
        Long l = code.getExpire();//验证码时间
        System.out.println("l = " + l);
    }*/
}
